package com.block.datapush.service.apipush;

import cn.hutool.core.text.UnicodeUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.common.enums.BaseTypeInfoEnum;
import com.block.common.model.param.PushApiKeyParam;
import com.block.common.service.IMemberCommonService;
import com.block.common.service.LogFactory;
import com.block.datapush.enums.PushStatusEnum;
import com.block.datapush.model.PushQueryParam;
import com.block.datapush.model.dto.NoticeParamDTO;
import com.block.datapush.model.result.ProtocolParamResult;
import com.block.datapush.model.result.PushApiResult;
import com.block.datapush.service.IPushApiService;
import com.block.datapush.service.apipush.utils.DianDaiUtil;
import com.block.datapush.service.apipush.utils.LingJuLiUtil;
import com.block.db.entity.MemberSourceInfo;
import com.block.db.entity.MerchantBaseInfo;
import com.block.db.mapper.MerchantDataPushInfoMapper;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 零距离
 */
@Service
public class LingJuLiPushApiService implements IPushApiService {

    private static final Logger log = LogFactory.getPushLog();

    @Autowired
    IMemberCommonService memberCommonService;

    @Override
    public PushApiResult checkUser(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {

        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);


        String queryUrl = pushApiKeyParam.getCheckUrl();
        String mobileMd5 = memberSourceInfo.getMobileMd5();

        Date now = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp = sdf.format(now);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("aid","crm");
        paramMap.put("act","checkPhone");
        paramMap.put("timestamp",timestamp);
        String noncestr = RandomUtil.randomNumbers(9);
        paramMap.put("noncestr",noncestr);

        String signStr = StrUtil.format("{}{}{}{}{}", "crm", "checkPhone", timestamp,noncestr, pushApiKeyParam.getApiKey());
        String signature = SecureUtil.md5(signStr).toLowerCase();
        paramMap.put("signature",signature);
        paramMap.put("c_phone",mobileMd5);



        log.info("【零距离】撞库开始【queryUrl】{}【paramMap】{}", queryUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(queryUrl)
                .header("Content-Type", "application/x-www-form-urlencoded")
                .form(paramMap)
                .execute().body();
        log.info("【零距离】撞库结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer status = resultObject.getInteger("status");
        String info = resultObject.getString("info");

        pushApiResult.setCode(String.valueOf(status));
        pushApiResult.setMessage(info);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(status), "0")) {
            pushApiResult.setPushSuccess(true);
        }
        return pushApiResult;
    }

    @Override
    public PushApiResult queryApprove(String orderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);
        pushApiResult.setOrderNo(orderNo);
        return pushApiResult;
    }

    @Override
    public PushApiResult registerUser(String orderNo, String outOrderNo, PushApiKeyParam pushApiKeyParam, MemberSourceInfo memberSourceInfo, JSONObject extJson) {
        PushApiResult pushApiResult = new PushApiResult();
        pushApiResult.setPushSuccess(false);

        String requestUrl = pushApiKeyParam.getRegisterUrl();



        Date now = new Date();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String timestamp = sdf.format(now);

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("aid","crm");
        paramMap.put("act","customerCreate");
        paramMap.put("timestamp",timestamp);
        String noncestr = RandomUtil.randomNumbers(9);
        paramMap.put("noncestr",noncestr);
        String signStr = StrUtil.format("{}{}{}{}{}", "crm", "customerCreate", timestamp,noncestr, pushApiKeyParam.getApiKey());
        String signature = SecureUtil.md5(signStr).toLowerCase();
        paramMap.put("signature",signature);
        paramMap.put("c_name",memberSourceInfo.getRealname());
        paramMap.put("c_phone",memberSourceInfo.getMobile());
        paramMap.put("c_sex",memberSourceInfo.getSex());
        paramMap.put("type","助贷01");

        String city = memberSourceInfo.getCity();
        if (StrUtil.endWith(city, "市")) {
            city = memberSourceInfo.getCity().substring(0, memberSourceInfo.getCity().length() - 1);
        }

        paramMap.put("c_city",city);
        paramMap.put("c_daikuanedu", LingJuLiUtil.getloan(memberSourceInfo.getLoanAmount()));
        paramMap.put("c_has_xinyongka", LingJuLiUtil.getxyk(memberSourceInfo.getXinyongka()));
        paramMap.put("c_has_fang", LingJuLiUtil.getHouse(memberSourceInfo.getHouse()));
        paramMap.put("c_has_che", LingJuLiUtil.getCar(memberSourceInfo.getCar()));
        paramMap.put("c_has_gongjijin", LingJuLiUtil.getGjj(memberSourceInfo.getGjj()));
        paramMap.put("c_has_shebao", LingJuLiUtil.getSocialInsurance(memberSourceInfo.getShebao()));
        paramMap.put("c_has_baodan", LingJuLiUtil.getbaodan(memberSourceInfo.getBaoxian()));
        paramMap.put("c_has_weilidai", 2);
        paramMap.put("c_faxinfangshi", 1);
        paramMap.put("c_shouru", LingJuLiUtil.getshouru(memberSourceInfo.getShouru()));
        paramMap.put("c_zhiye", BaseTypeInfoEnum.Gongzuo.getNameByValue(memberSourceInfo.getGongzuo()));





        log.info("【零距离】导单开始【requestUrl】{}【paramMap】{}", requestUrl, JSON.toJSONString(paramMap));
        String result = HttpUtil.createPost(requestUrl)
                .header("Content-Type", "application/x-www-form-urlencoded")
                .form(paramMap)
                .execute().body();
        log.info("【零距离】导单结束【result】{}", result);

        JSONObject resultObject = JSON.parseObject(result);
        Integer status = resultObject.getInteger("status");
        String info = resultObject.getString("info");

        pushApiResult.setCode(status + "");
        pushApiResult.setMessage(info);
        pushApiResult.setParamInfo(JSON.toJSONString(paramMap));
        pushApiResult.setResultInfo(result);

        if (StrUtil.equals(String.valueOf(status), "0")) {
            pushApiResult.setPushSuccess(true);
            pushApiResult.setSettleFlag(true);
            pushApiResult.setStatus(PushStatusEnum.SUCCESS.getValue());
            return pushApiResult;
        }

        pushApiResult.setPushSuccess(false);
        pushApiResult.setSettleFlag(false);
        pushApiResult.setStatus(PushStatusEnum.FAILED.getValue());

        return pushApiResult;
    }

    @Override
    public PushApiResult queryResult(PushQueryParam pushQueryParam, PushApiKeyParam pushApiKeyParam, String extJson) {
        PushApiResult pushApiResult = new PushApiResult();

        return pushApiResult;
    }

    @Override
    public NoticeParamDTO getNoticeResultParam(String infoId, Map<String, String[]> parameterMap, String paramContent) {
        NoticeParamDTO noticeParamDTO = new NoticeParamDTO();

        return noticeParamDTO;
    }

    @Override
    public JSONObject getNoticeResultReturn(Map<String, String[]> parameterMap, String paramContent, JSONObject extObject) {
        JSONObject jsonObject = new JSONObject();
        return jsonObject;
    }

    @Override
    public ProtocolParamResult getProtocolResult(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo) {
        return null;
    }



}
